package com.ebelter.bodyfatscale.ui.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.util.DipUtil;
import com.ebelter.btlibrary.util.ULog;

/**
 * 仪表盘 LB
 */

public class MyDashBoardView3 extends View {

    private static final String TAG = "MyDashBoardView3";

    public MyDashBoardView3(Context context) {
        this(context, null);
    }

    public MyDashBoardView3(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public float displayValue = 0;


    public void setDisplayValue(float displayValue) {
        this.displayValue = displayValue;
    }


    int mWidth;
    int mHeight;

    int halfWidth;
    int halfHeight;

    Paint mPaint;

    Paint mBaoShaPaint;
    Paint mOutAcrPaint;
    Paint mBitmapPaint;
    Bitmap waihuan_bitmap;
    Bitmap neihuan_bitmap;


    int innerCircleColor;//最中心的内圆颜色
    int whiteColor;
    int color_67abfe;
    int color_5074a4;
    float innerCircleRadius;//最中心的内圆半径
    float innerGuanCircleRadius;//最中心的内光圆半径

    float outsideCircleRadius;

    float keDuCircleRadius;
    float keDuTextCircleRadius;


    Rect mSrcRectNeiguan;
    Rect mDestRectNeiguan;

    Rect mSrcRectWaiguan;
    Rect mDestRectWaiguan;

    RectF waiArcLineRectF;
    RectF baoShaArcLineRectF;
    RectF outArcLineRectF;

    float keduX0;
    float keduY0;
    float keduX1;
    float keduY1;
    float keduX2;
    float keduY2;


    private static final String[] LB_KEDU_VALUES = {"0", "50", "100", "150", "200", "250", "300", "350", "400"};
    float keduValue0_XY[];
    float keduValue1_XY[];
    float keduValue2_XY[];
    float keduValue3_XY[];
    float keduValue4_XY[];
    float keduValue5_XY[];
    float keduValue6_XY[];
    float keduValue7_XY[];
    float keduValue8_XY[];


    double sin45 = Math.sin(Math.toRadians(45));
    double cos11_25 = Math.cos(Math.toRadians(11.25));
    double sin11_25 = Math.sin(Math.toRadians(11.25));
    double cos22_5 = Math.cos(Math.toRadians(22.5));
    double sin22_5 = Math.sin(Math.toRadians(22.5));
    double cos33_75 = Math.cos(Math.toRadians(33.75));
    double sin33_75 = Math.sin(Math.toRadians(33.75));

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ULog.i(TAG, "onMeasure()---屏幕的宽：" + DipUtil.getWindowWidth(getContext()) + "---屏幕的高：" + DipUtil.getWindowHeight(getContext()));
        ULog.i(TAG, "------1dp=" + dp2px(getContext(), 1) + "px----------------10dp=" + dp2px(getContext(), 10));
        int width = getMySize(100, widthMeasureSpec);
        int height = getMySize(100, heightMeasureSpec);
        ULog.i(TAG, "onMeasure()---控件的宽：" + width + "---控件的高：" + height);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画内光图
        canvas.drawBitmap(neihuan_bitmap, mSrcRectNeiguan, mDestRectNeiguan, mBitmapPaint);//0k

        //画外光图
        canvas.drawBitmap(waihuan_bitmap, mSrcRectWaiguan, mDestRectWaiguan, mBitmapPaint);//0k

        //画外弧的粗弧线
        drawWaiArcLine(canvas);//0k

        //画刻度线
        drawKedu(canvas);//ok

        //画薄纱
        drawBaoShaKeDu(canvas);//ok

        //画刻度箭头
        drawKeDuJianTou(canvas);

        //画中间的圆形
        drawInnerCircle(canvas);

        //画当前显示的值
        drawValue(canvas);

        //画向上向下箭头
        drawFXJianTou(canvas);

    }

    private void drawBaoShaKeDu(Canvas canvas) {

        if (displayValue > 0) {
            canvas.drawArc(baoShaArcLineRectF, 135, (float) (displayValue * animatorPersent * 27 / 40.0 - 0.8f), false, mBaoShaPaint);
            mOutAcrPaint.setShader(getOutArcShader());
            canvas.drawArc(outArcLineRectF, 135, (float) (displayValue * animatorPersent * 27 / 40.0), false, mOutAcrPaint);
        }


    }

    //画刻度箭头
    private void drawKeDuJianTou(Canvas canvas) {
        canvas.save();
        canvas.rotate((float) (-135 + displayValue * animatorPersent * 27 / 40.0), halfWidth, halfHeight);
        canvas.drawPath(keDuJianTouPath, mPaint);
        canvas.restore();
    }

    private void drawFXJianTou(Canvas canvas) {
        if (isDisplayFxjt && displayValue > 0) {


            if (isDisplayFxjt_XiangShang) {
                // canvas.drawPath(fxjt_XiangShang_Path,mPaint);
                drawJT(canvas, fxjt_XiangShang_Path);
            } else {
                //canvas.drawPath(fxjt_XiangXia_Path,mPaint);
                drawJT(canvas, fxjt_XiangXia_Path);
            }
        }
    }

    private void drawJT(Canvas canvas, Path path) {
        int moshu = ((int) (animatorPersent * 100)) % 20;
        if (moshu < 10) {
            canvas.drawPath(path, mPaint);
        }

    }


    //是否显示方向箭头的标识
    public boolean isDisplayFxjt = true;
    //箭头是否是向上
    public boolean isDisplayFxjt_XiangShang = true;

    Path fxjt_XiangShang_Path = new Path();
    Path fxjt_XiangXia_Path = new Path();

    //画当前显示的值
    float valueTextSize = dp2px(getContext(), 26.6f);
    float valueTextSize2 = dp2px(getContext(), 10f);

    private void drawValue(Canvas canvas) {
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setColor(whiteColor);
        mPaint.setTextSize(valueTextSize);
        canvas.drawText(String.format("%.1f", displayValue * animatorPersent).toString(), halfWidth, halfHeight + valueTextSize / 3, mPaint);
        mPaint.setTextSize(valueTextSize2);
        canvas.drawText("lb", halfWidth, halfHeight + valueTextSize / 3 + valueTextSize2 * 2, mPaint);

    }


    int textWidth1;
    int changkedu = dp2px(getContext(), 17);
    int duankedu = dp2px(getContext(), 10);
    int paintTextSize = dp2px(getContext(), 10);

    private void drawKedu(Canvas canvas) {
        mPaint.setColor(color_5074a4);
        mPaint.setTextSize(paintTextSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.save();
        for (int i = 0; i < 41; i++) {
            if (i % 5 == 0) {
                mPaint.setStrokeWidth(dp2px(getContext(), 2.7f));
                canvas.drawLine(keduX0, keduY0, keduX1, keduY1, mPaint);
            } else {
                mPaint.setStrokeWidth(dp2px(getContext(), 1.7f));
                canvas.drawLine(keduX0, keduY0, keduX2, keduY2, mPaint);
            }
            canvas.rotate(6.75f, halfWidth, halfHeight);
        }
        canvas.restore();

        mPaint.setColor(whiteColor);
        for (int i = 0; i < LB_KEDU_VALUES.length; i++) {
            String displayTitle = LB_KEDU_VALUES[i];
            float x = 0;
            float y = 0;
            switch (i) {
                case 0:
                    x = keduValue0_XY[0];
                    y = keduValue0_XY[1];
                    break;
                case 1:
                    x = keduValue1_XY[0];
                    y = keduValue1_XY[1];
                    break;
                case 2:
                    x = keduValue2_XY[0];
                    y = keduValue2_XY[1];
                    break;
                case 3:
                    x = keduValue3_XY[0];
                    y = keduValue3_XY[1];
                    break;
                case 4:
                    x = keduValue4_XY[0];
                    y = keduValue4_XY[1];
                    break;
                case 5:
                    x = keduValue5_XY[0];
                    y = keduValue5_XY[1];
                    break;

                case 6:
                    x = keduValue6_XY[0];
                    y = keduValue6_XY[1];
                    break;
                case 7:
                    x = keduValue7_XY[0];
                    y = keduValue7_XY[1];
                    break;
                case 8:
                    x = keduValue8_XY[0];
                    y = keduValue8_XY[1];
                    break;

            }
            canvas.drawText(displayTitle, x, y, mPaint);
        }

    }

    private void drawWaiArcLine(Canvas canvas) {
        mPaint.setColor(color_67abfe);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(dp2px(getContext(), 3));
        canvas.drawArc(waiArcLineRectF, 135, 270, false, mPaint); //绘制圆弧
    }

    //画内圆
    private void drawInnerCircle(Canvas canvas) {
        mPaint.setColor(innerCircleColor);
        canvas.drawCircle(halfWidth, halfHeight, innerCircleRadius, mPaint);
    }


    private void initBaseData() {
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        halfWidth = mWidth / 2;
        halfHeight = mHeight / 2;
        innerCircleRadius = mWidth * 0.1981f;
        innerGuanCircleRadius = mWidth * 0.2540f;
        outsideCircleRadius = mWidth * 0.4449f;

        keDuCircleRadius = mWidth * 0.4102f;

        keDuTextCircleRadius = mWidth * 0.30f;

        mPaint.setTextSize(paintTextSize);

        textWidth1 = getTextWidth(mPaint, "X");

        //innerGuanCircleRadius =innerCircleRadius+;

        //--------------mWidth=639---mHeight=639---halfWidth=319---halfHeight=319---innerCircleRadius=126.5859---innerGuanCircleRadius=143.136
        ULog.i(TAG, "----------initBaseData（）----mWidth=" + mWidth + "---mHeight=" + mHeight + "---halfWidth=" + halfWidth + "---halfHeight=" + halfHeight + "---innerCircleRadius=" + innerCircleRadius + "---innerGuanCircleRadius=" + innerGuanCircleRadius);

        mSrcRectNeiguan = new Rect(0, 0, neihuan_bitmap.getWidth(), neihuan_bitmap.getHeight());
        mDestRectNeiguan = new Rect((int) (halfWidth - innerGuanCircleRadius), (int) (halfHeight - innerGuanCircleRadius), (int) (halfWidth + innerGuanCircleRadius), (int) (halfHeight + innerGuanCircleRadius));

        mSrcRectWaiguan = new Rect(0, 0, waihuan_bitmap.getWidth(), waihuan_bitmap.getHeight());
        mDestRectWaiguan = new Rect((int) (halfWidth - outsideCircleRadius), (int) (halfHeight - outsideCircleRadius), (int) (halfWidth + outsideCircleRadius), (int) (halfHeight + outsideCircleRadius));


        waiArcLineRectF = new RectF(); //RectF对象
        waiArcLineRectF.left = halfWidth - outsideCircleRadius; //左边
        waiArcLineRectF.top = halfHeight - outsideCircleRadius; //上边
        waiArcLineRectF.right = halfWidth + outsideCircleRadius; //右边
        waiArcLineRectF.bottom = halfHeight + outsideCircleRadius; //下边

        keduX0 = (float) (halfHeight - sin45 * keDuCircleRadius);
        keduY0 = (float) (halfHeight + sin45 * keDuCircleRadius);

        keduX1 = (float) (halfHeight - sin45 * (keDuCircleRadius - changkedu));
        keduY1 = (float) (halfHeight + sin45 * (keDuCircleRadius - changkedu));

        keduX2 = (float) (halfHeight - sin45 * (keDuCircleRadius - duankedu));
        keduY2 = (float) (halfHeight + sin45 * (keDuCircleRadius - duankedu));


        keduValue0_XY = new float[2];
        keduValue1_XY = new float[2];
        keduValue2_XY = new float[2];
        keduValue3_XY = new float[2];
        keduValue4_XY = new float[2];
        keduValue5_XY = new float[2];
        keduValue6_XY = new float[2];
        keduValue7_XY = new float[2];
        keduValue8_XY = new float[2];


        keduValue0_XY[0] = (float) (halfHeight - sin45 * keDuTextCircleRadius);//0 ok
        keduValue0_XY[1] = (float) (halfHeight + sin45 * keDuTextCircleRadius);

        keduValue1_XY[0] = (float) (halfHeight - cos11_25 * keDuTextCircleRadius) + textWidth1 / 2;//50 ok
        keduValue1_XY[1] = (float) (halfHeight + sin11_25 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue2_XY[0] = (float) (halfHeight - cos22_5 * keDuTextCircleRadius) + textWidth1 / 2;//100 ok
        keduValue2_XY[1] = (float) (halfHeight - sin22_5 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue3_XY[0] = (float) (halfHeight - sin33_75 * keDuTextCircleRadius);//150 ok
        keduValue3_XY[1] = (float) (halfHeight - cos33_75 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue4_XY[0] = (float) (halfHeight);//200 ok
        keduValue4_XY[1] = (float) (halfHeight - keDuTextCircleRadius) + paintTextSize / 2;

        keduValue5_XY[0] = (float) (halfHeight + sin33_75 * keDuTextCircleRadius);//250 ok
        keduValue5_XY[1] = (float) (halfHeight - cos33_75 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue6_XY[0] = (float) (halfHeight + cos22_5 * keDuTextCircleRadius) - textWidth1 / 2;//300 ok
        keduValue6_XY[1] = (float) (halfHeight - sin22_5 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue7_XY[0] = (float) (halfHeight + cos11_25 * keDuTextCircleRadius) - textWidth1;//350 ok
        keduValue7_XY[1] = (float) (halfHeight + sin11_25 * keDuTextCircleRadius) + paintTextSize / 2;

        keduValue8_XY[0] = (float) (halfHeight + sin45 * keDuTextCircleRadius) - textWidth1;//400
        keduValue8_XY[1] = (float) (halfHeight + sin45 * keDuTextCircleRadius) + paintTextSize / 2;


        jianTouPanddingTop = innerCircleRadius * 0.3f;
        fxjt_XiangShang_Path.reset();
        fxjt_XiangShang_Path.moveTo(halfWidth, halfHeight - innerCircleRadius + jianTouPanddingTop);//01
        fxjt_XiangShang_Path.lineTo(halfWidth + jianTouWidth / 2, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight * 0.425f);//02
        fxjt_XiangShang_Path.lineTo(halfWidth + jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight * 0.425f);//03
        fxjt_XiangShang_Path.lineTo(halfWidth + jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight);//04
        fxjt_XiangShang_Path.lineTo(halfWidth - jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight);//05
        fxjt_XiangShang_Path.lineTo(halfWidth - jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight * 0.425f);//06
        fxjt_XiangShang_Path.lineTo(halfWidth - jianTouWidth / 2, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight * 0.425f);//02
        fxjt_XiangShang_Path.lineTo(halfWidth, halfHeight - innerCircleRadius + jianTouPanddingTop);//02
        fxjt_XiangShang_Path.close();

        fxjt_XiangXia_Path.reset();
        fxjt_XiangXia_Path.moveTo(halfWidth, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight);//01
        fxjt_XiangXia_Path.lineTo(halfWidth + jianTouWidth / 2, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight - jianTouHeight * 0.425f);//02
        fxjt_XiangXia_Path.lineTo(halfWidth + jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight - jianTouHeight * 0.425f);//03
        fxjt_XiangXia_Path.lineTo(halfWidth + jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop);//04
        fxjt_XiangXia_Path.lineTo(halfWidth - jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop);//05
        fxjt_XiangXia_Path.lineTo(halfWidth - jianTouWidth / 5, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight - jianTouHeight * 0.425f);//06
        fxjt_XiangXia_Path.lineTo(halfWidth - jianTouWidth / 2, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight - jianTouHeight * 0.425f);//07
        fxjt_XiangXia_Path.lineTo(halfWidth, halfHeight - innerCircleRadius + jianTouPanddingTop + jianTouHeight);//02
        fxjt_XiangXia_Path.close();


        keDuJianTouPath = new Path();
        keDuJianTouPath.reset();
        //  keDuJianTouPath.moveTo(0,0);
        keDuJianTouPath.moveTo(halfWidth, halfHeight - keDuCircleRadius + duankedu);
        keDuJianTouPath.lineTo(halfWidth + 10, halfHeight);
        keDuJianTouPath.lineTo(halfWidth - 10, halfHeight);
        keDuJianTouPath.lineTo(halfWidth, halfHeight - keDuCircleRadius + duankedu);
        keDuJianTouPath.close();


        baoShaArcLineRectF = new RectF();
        float baoShaPaintStroWidth = (outsideCircleRadius - innerCircleRadius) * 105f / 133f;
        float temp = dp2px(getContext(), 1.7f);
        baoShaArcLineRectF.left = halfWidth - outsideCircleRadius + baoShaPaintStroWidth / 2 + temp; //左边 5是外弧线的粗度的一半
        baoShaArcLineRectF.top = halfHeight - outsideCircleRadius + baoShaPaintStroWidth / 2 + temp; //上边
        baoShaArcLineRectF.right = halfWidth + outsideCircleRadius - baoShaPaintStroWidth / 2 - temp; //右边
        baoShaArcLineRectF.bottom = halfHeight + outsideCircleRadius - +baoShaPaintStroWidth / 2 - temp; //下边

        outArcLineRectF = new RectF();
        float outPaintStroWidth = dp2px(getContext(), 6.7f);
        outArcLineRectF.left = halfWidth - outsideCircleRadius - outPaintStroWidth; //左边
        outArcLineRectF.top = halfHeight - outsideCircleRadius - outPaintStroWidth; //上边
        outArcLineRectF.right = halfWidth + outsideCircleRadius + outPaintStroWidth; //右边
        outArcLineRectF.bottom = halfHeight + outsideCircleRadius + outPaintStroWidth; //下边


        mBaoShaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBaoShaPaint.setStyle(Paint.Style.STROKE);
        int color0 = getResources().getColor(R.color.bg_chiffon_dashboard_blue_transparent_0);
        int color1 = getResources().getColor(R.color.bg_chiffon_dashboard_blue_transparent_2);
//        int color0 = getResources().getColor(R.color.bg_white);
//        int color1 = getResources().getColor(R.color.bg_black);
        baoshaColors = new int[]{color1, color0};
        mBaoShaPaint.setStrokeWidth(baoShaPaintStroWidth);
        mBaoShaPaint.setAlpha(180);


        //先创建一个渲染器,扫描渐变
        SweepGradient mSweepGradient = new SweepGradient(halfWidth, halfHeight, baoshaColors, new float[]{0f, 1f});
        //旋转渐变
        Matrix matrix = new Matrix();
        matrix.setRotate(45, halfWidth, halfHeight);
        mSweepGradient.setLocalMatrix(matrix);
        //把渐变设置到笔刷
        mBaoShaPaint.setShader(mSweepGradient);

        //先创建一个渲染器,扫描渐变
        int white_color = getResources().getColor(R.color.bg_white);
        int black_color = getResources().getColor(R.color.bg_black);
        baoshaColors2 = new int[]{black_color, white_color};
//        SweepGradient mSweepGradient2 = new SweepGradient(halfWidth, halfHeight, baoshaColors2, new float[]{0f, 0.3f});
        //旋转渐变
//        Matrix matrix2 = new Matrix();
//        matrix2.setRotate(45, halfWidth, halfHeight);
//        mSweepGradient2.setLocalMatrix(matrix2);


        mOutAcrPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mOutAcrPaint.setStyle(Paint.Style.STROKE);
        mOutAcrPaint.setStrokeWidth(dp2px(getContext(), 3.3f));
        // mOutAcrPaint.setShader(mSweepGradient2);
        mOutAcrPaint.setStrokeCap(Paint.Cap.ROUND);


    }

    int[] baoshaColors2;

    Path keDuJianTouPath;

    float jianTouWidth = dp2px(getContext(), 10f);
    float jianTouHeight = dp2px(getContext(), 13.3f);
    float jianTouPanddingTop;


    public int getTextWidth(Paint paint, String str) {
        int w = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                w += (int) Math.ceil(widths[j]);
            }
        }
        return w;
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initBaseData();
    }

    int[] baoshaColors;

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mBitmapPaint.setStyle(Paint.Style.FILL);


        waihuan_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dash_waihuan);
        neihuan_bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dash_neihuan);
        ULog.i(TAG, "init()-waihuan_bitmap.getWidth()=" + waihuan_bitmap.getWidth() + "---waihuan_bitmap.getHeight()=" + waihuan_bitmap.getHeight());
        ULog.i(TAG, "init()-neihuan_bitmap.getWidth()=" + neihuan_bitmap.getWidth() + "---neihuan_bitmap.getHeight()=" + neihuan_bitmap.getHeight());

        innerCircleColor = Color.parseColor("#1f2839");//最中心的内圆颜色
        whiteColor = Color.parseColor("#ffffff");
        color_67abfe = Color.parseColor("#67abfe");
        color_5074a4 = Color.parseColor("#5074a4");
    }

    private int getMySize(int defaultSize, int measureSpec) {
        int mySize = defaultSize;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);//这里的的尺寸大小并不是最终我们的View的尺寸大小，而是父View提供的参考大小
        switch (mode) {
            case MeasureSpec.UNSPECIFIED: //如果没有指定大小，就设置为默认大小
                mySize = defaultSize;
                break;

            case MeasureSpec.AT_MOST: //如果测量模式是最大取值为size
            case MeasureSpec.EXACTLY: //如果是固定的大小，那就不要去改变它
                //我们将大小取最大值,你也可以取其他值
                mySize = size;
                break;


        }
        return mySize;
    }

    public int dp2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    ValueAnimator animator;
    public boolean isDisPlayAnimation = true;

    float animatorPersent = 1.0f;//动画百分比

    public void show() {

        if (isDisPlayAnimation) {

            if (animator != null) {
                animator.cancel();
                animator = null;
            }


            animator = ValueAnimator.ofFloat(0.0f, 1.0f);
            animator.setDuration(2000);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    animatorPersent = (float) animation.getAnimatedValue();
                    invalidate();

                }
            });
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationCancel(Animator animation) {
                    super.onAnimationCancel(animation);
                    animatorPersent = 1.0f;
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    animatorPersent = 1.0f;
                }
            });
            animator.start();
        } else {
            invalidate();
        }
    }


    public SweepGradient getOutArcShader() {
        SweepGradient sweepGradient = new SweepGradient(halfWidth, halfHeight,
                baoshaColors2,
                //new float[]{0, calculateRelativeAngleWithValue(mCreditValue) / 360}
                new float[]{0, displayValue / 360f}
        );
        Matrix matrix = new Matrix();
        matrix.setRotate(135 - 1, halfWidth, halfWidth);
        sweepGradient.setLocalMatrix(matrix);

        return sweepGradient;

    }
}
